using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using UnityEngine;
using UnityEngine.Serialization;

public interface Form
{
    /// <summary>
    /// 打开选择文件 多选
    /// </summary>
    /// <param name="folderPath">初始文件目录路径</param>
    /// <param name="filter">类型筛选</param>
    /// <param name="callBack">返回文件</param>
    public void SelectFile(string folderPath = null, string filter = null, System.Action<FileData> callBack = null);

 
   /// <summary>
   /// 打开选择文件 保存 
   /// </summary>
   /// <param name="fromPath">源文件路径</param>
   /// <param name="folderPath"></param>
   /// <param name="filter"></param>
   /// <param name="callBack"></param>
    public void SelectSaveFile(string fromPath = null, string folderPath = null, string filter = null,
        Action<string, bool> callBack = null);

    /// <summary>
    /// 打开选择 文件夹
    /// </summary>
    /// <param name="folderPath">>初始文件目录路径</param>
    /// <param name="callBack">文件夹路径</param>
    public void SelectFolder(string folderPath = null, System.Action<string> callBack = null);


    /// <summary>
    /// 打开 文件夹
    /// </summary>
    /// <param name="path"></param>
    public void OpenFolder(string path);
}

public struct FileData
{
    public string FolderPath;
    public string[] FilePath;
    public string[] FileName;

    private string fileData;

    public string FileDatas
    {
        set
        {
            fileData = value;
            FolderPath = null;
            FilePath = null;
            FileName = null;


            string[] Splitstr = { "\0" };
            string[] strs = value.Split(Splitstr, StringSplitOptions.RemoveEmptyEntries);
            int len = strs.Length;

            if (len == 1) //文件 全路径
            {
                string path = strs[0].Replace("\\", "/");
                FilePath = new[] { path };

                int index = path.LastIndexOf('/');
                FolderPath = path.Substring(0, index);

                index++;
                string fileName = path.Substring(index, path.Length - index);
                FileName = new[] { fileName };
            }
            else if (len > 1)
            {
                len -= 1;
                FolderPath = strs[0].Replace("\\", "/"); //0  文件夹 全路径 
                FileName = new string[len]; //>=1  文件名
                FilePath = new string[len];

                for (int i = 0; i < len; i++)
                {
                    FileName[i] = strs[i + 1];
                    FilePath[i] = FolderPath + "/" + strs[i + 1];
                }
            }
        }
    }

    public string ToString()
    {
        StringBuilder selectFileBuilder = GetStringBuilder(FilePath);
        StringBuilder selectFileNameBuilder = GetStringBuilder(FileName);

        string str =
            $"Folder: {FolderPath}\r\nFile: {selectFileBuilder.ToString()}\r\nFileName: {selectFileNameBuilder.ToString()}";
        Debug.Log(str);
        return str;
    }

    private StringBuilder GetStringBuilder(string[] arr)
    {
        StringBuilder builder = new StringBuilder();
        if (arr != null)
        {
            int len = arr.Length;
            for (int i = 0; i < len; i++)
            {
                builder.AppendLine(arr[i]);
            }
        }

        return builder;
    }
}